what is logical operator in c++? logical AND\OR\NOT operators explained.

 

what is the logical operator in c++?

How do we use logical operators in c++?

Logical Operators Explained.


Logical Operators 

A logical operator is a symbol that is used in complex logical expressions by combining two or more two simple logical expressions.

It connects two or more expressions such that the value of the compound expression. Logical operators are used to evaluate the compound condition or the reverse of the logic expression. The logical operator depends on the original expression and the means of operator what types of operators are used to evaluate the expression of the program. The logical operator helps the test expression which controls the flow of the program. A logical operator is the most important feature of the program it is used in a different language to create the program.

Consider the following table of different operators for priority,

Table of the logical operator



The logical operators have three basic types that have been used in the daily program.

Types of logical  operator

  • AND Operator (&&)
  • OR Operator (||)
  • NOT Operator (!)




  • AND Operator ( && ).

The AND operator is used to evaluate two conditions. The symbol of the AND operator is ( &&) . it gives the true result if both of the conditions are true. If the condition of both is true then the AND operator gives a true result. it shows a false result if any one of the condition is false.

Example

    Let us have two variables A = 10 and B =5. The compound condition (A>40)   && (B>4) is true. It contains two conditions and both are true. So the whole compound condition is also true.

 and 

Let us have two variables A = 10 and B =5. The compound condition (A<4)   && (B>4) is true. It contains two conditions one condition is true and the other condition is false if the one condition Is false then the whole compound will be false.




AND operator
AND operator




  • OR operator ( || ).

OR operator is the type of the logical operator but it is opposite to the AND operator. The symbol used for the OR operator is (||). It also evaluates two conditions. It gives the true result if one of the conditions is true then it gives the true result. If both are the condition is false then it gives false results.

In AND operator we have a true result if both conditions are true now in the OR operator show a true result if one of the condition is true.

Example

Let us have two variables A = 10 and B =5. The compound condition (A>12)   ||  (B>4) is true. It contains two conditions the one is true and the other is false. In the OR operator if one condition is true then the whole compound will the true. If both are false then in this case the whole compound will be false.

 


OR operator
OR operator


  • NOT operator ( ! ).

NOT operator is the type of logical operator. The symbol of the NOT operator is (!). it is used in the program to reverse the result of the condition. It produced the true result if the condition is false. NOT operator result is opposite of the condition if the condition is true then the result is false and vice versa.

Example.

  Let us have two variables A = 10 and B =5. The compound condition !(A==B) is true. It gives the true result of (A==B) is false but the NOT operator converts it into true. It produced a false result if the condition is true. It means the NOT operator used for opposite true to false and false to true.

 


NOT operator
NOT operator



Explanation of the 
if statement  
                            Click here 

if...else statement 
                 Click here 

Networking device explained